package com.inspectortime.webapp;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.inspectortime.domain.Inspection;
import com.inspectortime.domain.type.State;
import com.inspectortime.repository.Factory;
import com.inspectortime.repository.Repository;
import com.inspectortime.util.SpringValidatorUtils;
import com.inspectortime.webapp.util.CustomStateEditor;
import com.inspectortime.webapp.util.HttpSessionUtils;

public class InspectionCustomerInformationController extends SimpleFormController {
	
	private Factory factory;
	public void setFactory(Factory factory) {
		this.factory = factory;
	}

	public void setRepository(Repository repository) {
		this.repository = repository;
	}

	private Repository repository;

	private static Log log = LogFactory.getLog(InspectionCustomerInformationController.class);

	public InspectionCustomerInformationController() {
		super();
	}
	
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		InspectionCriteria inspectionCriteria = new InspectionCriteria();
		Inspection inspection = HttpSessionUtils.retrieveWizardInspection(request, repository);
		inspectionCriteria.setInspection(inspection);
		
		return inspectionCriteria;
	}
	
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {

		InspectionCriteria inspectionCriteria = (InspectionCriteria) command;

		inspectionCriteria.getInspection().save();
		
		return new ModelAndView(this.getSuccessView());
	}
	
	/**
	 * Override empty parent method to log validation errors for debugging
	 * purposes only. Can be removed.
	 */
	protected void onBindAndValidate(HttpServletRequest request,
			Object command, BindException errors) throws Exception {
		SpringValidatorUtils.logValidationErrors(log, errors);
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("states", State.values());
		return data;
	}

	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		// Feb 4, 2008 8:00:00 AM
		DateFormat df = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a");
		df.setLenient(true);
		CustomDateEditor editor = new CustomDateEditor(df, false);
		binder.registerCustomEditor(Date.class, editor);
		binder.registerCustomEditor(State.class, new CustomStateEditor());
	}


}
